package com.flismax.mysms.activity.sms;

import java.util.ArrayList;
import java.util.List;

import com.flismax.mysms.R;
import com.flismax.mysms.sms.control.Sms;


import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class SearchSMS extends Activity{
	List<Sms> model=new ArrayList<Sms>();
	EditText tsearch=null;
	Button bsearch=null;
	SMSAdapter adapter=null;
	@Override
	   protected void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      setContentView(R.layout.searchsms);
	     
	      tsearch=(EditText)findViewById(R.id.searchText);
	      bsearch=(Button)findViewById(R.id.bsearch);
	      ListView list=(ListView)findViewById(R.id.listSMS);
	      adapter=new SMSAdapter();
	      list.setAdapter(adapter);
	      bsearch.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				adapter.clear();
				 Uri uriSMSURI = Uri.parse("content://sms/inbox");
			      Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
			      //String sms = "";
			      String ts=tsearch.getText().toString();
			      while (cur.moveToNext()) {
			    	  if (timkiemSMS(cur.getString(11),ts)){
			    		  //sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+ ":"+ts+"\n";
			    		  Sms s=new Sms();
				    	  s.setAddress(cur.getString(2));
				    	  s.setBody(cur.getString(11));
				    	  s.setDate(System.currentTimeMillis());
				    	  adapter.add(s);
			    	  }	 
			      }
			     
				
			}
		});
		}
	public boolean timkiemSMS(String s,String s1){//tim kiem xau s1 trong s
		int n=s.length();
		int m=s1.length(),j;
		char ss,ss1;
		if (m>n) return false;
		for (int i=0;i<=n-m;i++){
			for (j=0;j<m;j++){
				ss=s.charAt(i+j);
				ss1=s1.charAt(j);
				if (ss!=ss1) break;	
				if (j==m-1) return true;
			}
		}
		return false;
	}
	class SMSAdapter extends ArrayAdapter<Sms> {
		SMSAdapter() {
			super(SearchSMS.this, R.layout.row, model);
		}
		public View getView(int position, View convertView,
                        ViewGroup parent) {
			View row=convertView;
			SMSHolder holder=null;
      
			if (row==null) {                          
				LayoutInflater inflater=getLayoutInflater();
				row=inflater.inflate(R.layout.row, parent, false);
				holder=new SMSHolder(row);
				row.setTag(holder);
			}
			else {
				holder=(SMSHolder)row.getTag();
			}
      
			holder.populateFrom(model.get(position));
      
			return(row);
		}
	}
	  
	  static class SMSHolder {
	    private TextView sdt=null;
	    private TextView noidung=null;
	    private ImageView icon=null;
	    
	    SMSHolder(View row) {
	      sdt=(TextView)row.findViewById(R.id.sdt);
	      noidung=(TextView)row.findViewById(R.id.noidung);
	      icon=(ImageView)row.findViewById(R.id.icon);
	    }
	    
	    void populateFrom(Sms r) {
	      sdt.setText(r.getAddress());
	      noidung.setText(r.getBody()+" "+r.getDate());
	      int i=(int) (Math.random()*3);
	      if (i==0) {
	        icon.setImageResource(R.drawable.ball_red);
	      }
	      else if (i==2) {
	        icon.setImageResource(R.drawable.ball_yellow);
	      }
	      else {
	        icon.setImageResource(R.drawable.ball_green);
	      }
	    }
	  }
}
